package org.boulderfoodrescue.webapp.client.recipients;

import java.util.List;

import org.boulderfoodrescue.webapp.client.Presenter;
import org.boulderfoodrescue.webapp.client.donors.DonorRecipientInterface;
import org.boulderfoodrescue.webapp.client.donors.SetLocationInfoView;
import org.boulderfoodrescue.webapp.client.events.RecipientInfoUpdatedEvent;
import org.boulderfoodrescue.webapp.client.events.RecipientInfoUpdatedHandler;
import org.boulderfoodrescue.webapp.client.managers.LocationInfoManager;
import org.boulderfoodrescue.webapp.client.pickuptimes.TimeSlotView;
import org.boulderfoodrescue.webapp.shared.Location;
import org.boulderfoodrescue.webapp.shared.Location.LocationType;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class RecipientPresenter implements Presenter, DonorRecipientInterface{
	private EventBus eventBus;
	private FlowPanel container;
	private MainRecipientView mainRecipientView;
	//	private FindAddressView findAddressView;
	private SetLocationInfoView setLocationInfoView;
	private TimeSlotView timeSlotView;

	public RecipientPresenter(EventBus eventBu2, String token)
	{  
		this.mainRecipientView = new MainRecipientView(this);
		//		this.findAddressView = new FindAddressView(this);
		this.setLocationInfoView = new SetLocationInfoView(LocationType.Recipient);

		container = new FlowPanel();
		container.add(mainRecipientView);
		container.setHeight("100%");

		this.eventBus = eventBu2;
		bind();

		List<Location> donors = LocationInfoManager.getRecipientList();
		for(Location d : donors){
			mainRecipientView.getRecipientTable().addLocation(d);
		}

		if(token.equals("recipientsNewRecipient"))
			onCreateNewLocationClick();
	}

	private void bind()
	{
		eventBus.addHandler(RecipientInfoUpdatedEvent.getType(),
				new RecipientInfoUpdatedHandler(){
			@Override
			public void onRecipientAdded(RecipientInfoUpdatedEvent event) {
				mainRecipientView.getRecipientTable().addLocation(event.getValue());
			}
			@Override
			public void onRecipientRemoved(RecipientInfoUpdatedEvent event) {
				mainRecipientView.getRecipientTable().removeLocation(event.getValue());
			}
			@Override
			public void onRecipientUpdated(RecipientInfoUpdatedEvent event) {
				mainRecipientView.getRecipientTable().updateLocation();
			}
		});
		setLocationInfoView.addValueChangeHandler(new ValueChangeHandler<Location>(){
			@Override
			public void onValueChange(ValueChangeEvent<Location> event) {
				container.clear();
				container.add(mainRecipientView);
				if(event.getValue() != null)
					LocationInfoManager.saveLocationInfo(event.getValue());
				History.newItem("recipients", false);
			}
		});
	}

	@Override
	public Widget getWidget() {
		return container;
	}

	@Override
	public void onCreateNewLocationClick() {
		container.clear();
		container.add(setLocationInfoView);
		setLocationInfoView.reset();
	}

	@Override
	public void onCancelNewLocation() {	
		container.clear();
		container.add(mainRecipientView);
	}


	@Override
	public void onTimeSlotViewOpen(Location location) {
		container.clear();
		this.timeSlotView = new TimeSlotView(this, eventBus, location);
		container.add(timeSlotView);
	}

	@Override
	public void onTimeStotViewClose() {
		container.clear();
		container.add(mainRecipientView);
	}

	@Override
	public void onEditLocation(Location location) {
		container.clear();
		container.add(setLocationInfoView);
		setLocationInfoView.setValue(location, false);
	}

}
